package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"info-hub/model"

	"info-hub/inforpc/inforpc"
	"info-hub/inforpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpDataLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpDataLogic {
	return &UpDataLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpDataLogic) UpData(in *inforpc.UpDataRequest) (*inforpc.UpDataResponse, error) {
	// todo: add your logic here and delete this line
	var user model.User
	model.DB.Where("account", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户未登录")
	}
	user = model.User{
		Model: gorm.Model{
			ID: uint(in.UserId),
		},
		Avatar: in.Avatar,
		Add:    in.Add,
		Name:   in.Name,
	}
	model.DB.Updates(&user)
	return &inforpc.UpDataResponse{
		UserId: int64(user.ID),
	}, nil
}
